<?php

class Mfriend extends CI_Model {
    public function sndRequest($adder,$addee){
        $adder = mysql_real_escape_string($adder);
        $addee = mysql_real_escape_string($addee);
        $check = $this->db->query("SELECT friend_id FROM tbl_friend WHERE (UserA=$adder AND UserB=$addee) OR (UserA=$addee AND UserB=$adder)");
       
        if($adder && $addee && $check->num_rows() == 0 && $this->getRelationship($adder,$addee) == -1){
            $status = false; //pending
            $date = date("Y/m/d");
            
            $tblData = array(
                'UserA' => $adder,
                'UserB' => $addee,
                'status' => $status,
                'date_added' => $date
            );
            
            $query = $this->db->insert('tbl_friend', $tblData);
            
            if($query) {
                $req = 1;
            }
        } else if($check->num_rows() > 0 && $this->getRelationship($adder,$addee) == 0) {
            $req = 2;
        } else if($check->num_rows() > 0 && $this->getRelationship($adder,$addee) == 1) {
            $req = 3;
        }
        
        return $req;
    }
    
    public function getRelationship($userA,$userB) {
        $userA = mysql_real_escape_string($userA);
        $userB = mysql_real_escape_string($userB);
        $query = $this->db->query("SELECT UserA,status FROM tbl_friend WHERE (UserA=$userA AND UserB=$userB) OR (UserA=$userB AND UserB=$userA)");
        $result = $query->result();
        
        if($query->num_rows() > 0) {
            if($result[0]->status == 1) {
                $status = 1;
            } else if($result[0]->UserA == $userA) {
                $status = 0;
            } else {
                $status = 2;
            }
        } else {
            $status = -1;
        }
        
        return $status;
    }
    
    /*NO USE FOR THE MOMENT--------------------------------------------
    -------------------------------------------------------------------
    public function friendRequest($userB) {
        $userB = mysql_real_escape_string($userB);
        $status = 0; //pending
        
        $query = $this->db->query("SELECT UserA,UserB,tbl_profile.profile_id,tbl_profile.f_name,tbl_profile.l_name,tbl_profile.about_me,tbl_profile.avatar FROM tbl_friend, tbl_profile WHERE tbl_friend.UserB = $userB AND tbl_friend.status=$status AND tbl_profile.profile_id = tbl_friend.UserA");
        $result = $query->result();
        
        if($query->num_rows() > 0) {
            if($result[0]->UserB == $userB) {
                $result;
            } else {
                $result = null;
            }
        }
        return $result;
    }
    -----------------------------------------------------------------
    -----------------------------------------------------------------*/
    
    public function apprvRequest($userA,$userB) {
        $userA = mysql_real_escape_string($userA);
        $userB = mysql_real_escape_string($userB);
        
        if($userA && $userB && $this->getRelationship($userA,$userB) == 0) {
            $status = 1; //accepted
            $date = date("Y/m/d h:i:s a", time());
            
            $tblSet = array('status' => $status);
            $tblWhere = array(
                'UserA' => $userA,
                'UserB' => $userB
            );
            
            $query = $this->db->update('tbl_friend',$tblSet,$tblWhere);
        }
        if($query){
            $noti = 1;
        }
        return $noti;
    }
    
    public function rjectRequest($userA,$userB) {
        $userA = mysql_real_escape_string($userA);
        $userB = mysql_real_escape_string($userB);
        
        if($userA && $userB && $this->getRelationship($userA,$userB) == 0) {
            $status = 1; //accepted
            $date = date("Y/m/d h:i:s a", time());
            
            $tblData = array(
                'UserA' => $userA,
                'UserB' => $userB
            );
            
            $query = $this->db->delete('tbl_friend',$tblData);
        }
        if($query){
            $noti = 1;
        }
        return $noti;
    }
}

?>